package cn.yunhe.servlet;

import cn.yunhe.biz.NewsService;
import cn.yunhe.pojo.Admin;
import cn.yunhe.pojo.Board;
import cn.yunhe.pojo.News;
import cn.yunhe.util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/AddNewsServlet")
public class AddNewsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Admin admin= (Admin) session.getAttribute("admin");

        String title = request.getParameter("title");
        String context = request.getParameter("context");
        String writeTime=DateUtil.getNowTime();
        int boardid= Integer.parseInt(request.getParameter("boardid"));
        Board board = new Board();
        board.setBoardId(boardid);

        News news = new News(title,context,writeTime,admin,board);
        //创建service ,传递news对象
        NewsService newsService = new NewsService();
        int flag = newsService.addNews(news);
        if (flag>0){
            response.sendRedirect("ToMainServlet");
        }else {
            response.sendRedirect("addNews.jsp");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }
}
